package com.xu.tao.demos.base;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class App {

    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void init() throws IOException {
        String resource = "mybatis/base.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void insertStudent(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        SqlSession sqlSession3 = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setAge(14);
        student.setName("XiaoYi");
        mapper.selectByPrimaryKey(3);
        mapper.insert(student);
        sqlSession.commit();
        System.out.println("OK");
    }

    @Test
    public void insertGrade(){
        Grade grade1 = new Grade();
        Grade grade2 = new Grade();
        Grade grade3 = new Grade();
        Grade grade4 = new Grade();
        grade1.setClassId(1);
        grade2.setClassId(2);
        grade3.setClassId(3);
        grade4.setClassId(4);
        grade1.setScore(60L);
        grade2.setScore(70L);
        grade3.setScore(80L);
        grade4.setScore(90L);
        grade1.setStudentId(6);
        grade2.setStudentId(6);
        grade3.setStudentId(6);
        grade4.setStudentId(6);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GradeMapper mapper = sqlSession.getMapper(GradeMapper.class);
        mapper.insert(grade1);
        mapper.insert(grade2);
        mapper.insert(grade3);
        mapper.insert(grade4);
        sqlSession.commit();
    }


    @Test
    public void insertClasses(){
        Classes classes1 = new Classes();
        Classes classes2 = new Classes();
        Classes classes3 = new Classes();
        Classes classes4 = new Classes();
        classes1.setName("English");
        classes2.setName("Chinese");
        classes3.setName("Math");
        classes4.setName("Nature");
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ClassesMapper mapper = sqlSession.getMapper(ClassesMapper.class);
        mapper.insert(classes1);
        mapper.insert(classes2);
        mapper.insert(classes3);
        mapper.insert(classes4);
        sqlSession.commit();
    }



}
